package com.teamwhole.modules.sys.web;

import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.teamwhole.common.config.Global;
import com.teamwhole.common.model.ResultInfo;
import com.teamwhole.modules.sys.entity.Menu;
import com.teamwhole.modules.sys.service.MenuService;
import com.teamwhole.modules.sys.utils.UserUtils;

@Component
@Path("/sys/menu")
@Consumes("application/json;charset=utf-8")
@Produces("application/json;charset=utf-8")
public class MenuResource {
	
	@Autowired
	private MenuService menuService;
	
	@Path("/get")
	@GET
	public Menu get(@QueryParam("id") String id) {
		return menuService.get(id);
	}
	
	@Path("/list")
	@POST
	public List<Menu> list(Menu model) {
		return menuService.findList(model);
	}
	
	@Path("/list-mine")
	@POST
	public List<Menu> listMine(Menu entity) {
		List<Menu> menus = Lists.newArrayList();
		List<Menu> list = UserUtils.getMenuList();
		for (Menu menu : list) {
			if (Global.YES.equals(menu.getIsShow()) && Global.NO.equals(menu.getIsButton()) && !Global.NO.equals(menu.getParentId())) {
				menus.add(menu);
			}
		}
		return menus;
	}
	
	@Path("/create")
	@POST
	public ResultInfo create(Menu model) {
		menuService.save(model);
		return ResultInfo.ok();
	}
	
	@Path("/update")
	@POST
	public ResultInfo update(Menu model) {
		menuService.save(model);
		return ResultInfo.ok();
	}
	
	@Path("/delete")
	@GET
	public ResultInfo delete(@QueryParam("id") String id) {
		menuService.delete(id);
		return ResultInfo.ok();
	}
}
